def move_zero_end2(list_target):
    new_list = [item for item in list_target if item != 0]
    new_list += [0] * list_target.count(0)
    # 如果不返回值，只改原来列表，可以这样操作
    # list_target[:] = new_list
    return new_list


def move_zero_end3(list_target):
    """
        将0移动到末尾
    :param list_target:
    :return:
    """
    for i in range(len(list_target) - 1, -1, -1):
        if list_target[i] == 0:
            del list_target[i]
            list_target.append(0)
    return list_target


def move_zero_end(*args):
    """
        将0移动到末尾
    :param args:
    :return:
    """
    args = list(args)
    for i in range(len(args) - 1, -1, -1):
        for j in range(0, i):
            if args[i] == 0:
                break
            if args[j] == 0:
                args[j], args[i] = args[i], args[j]
                break
    return args


def merge(list_target):
    """
        合并相邻且一样的数
    :param list_target:
    :return:
    """
    move_zero_end3(list_target)
    for i in range(len(list_target) - 1):
        if list_target[i] == list_target[i + 1]:
            list_target[i] += list_target[i + 1]
            list_target[i + 1] = 0
    move_zero_end3(list_target)


def move_left(list_target):
    """
        将二维数组的行合并数向左移动
    :param list_target:
    :return:
    """
    for arr in list_target:
        merge(arr)
    return list_target


def move_top(list_target):
    """
        将二维数组的行合并数向上移动
    :param list_target:
    :return:
    """

    for col in range(len(list_target[0])):
        todo_list = []
        for row in range(len(list_target)):
            todo_list.append(list_target[row][col])
        merge(todo_list)
        for row in range(len(list_target)):
            list_target[row][col] = todo_list[row]
    return list_target


def move_end(list_target):
    """
        将二维数组的行合并数向下移动
    :param list_target:
    :return:
    """

    for col in range(len(list_target[0])):
        todo_list = []
        for row in range(len(list_target)):
            todo_list.append(list_target[row][col])
        todo_list = todo_list[::-1]
        merge(todo_list)
        todo_list = todo_list[::-1]
        for row in range(len(list_target)):
            list_target[row][col] = todo_list[row]
    return list_target


def output(list_target):
    """
        将二维数组输出到控制台
    :param list_target:
    :return:
    """
    for arr in list_target:
        print(" ".join([str(i) for i in arr]))


print(move_zero_end(*[1, 3, 8, 0, 4, 9, 0]))
print(move_zero_end2([1, 3, 8, 0, 4, 9, 0]))
print(move_zero_end3([1, 3, 8, 0, 4, 9, 0]))

list01 = [2, 2, 1, 2, 0]
merge(list01)
print(list01)

list02 = [
    [2, 0, 0, 2],
    [2, 2, 0, 0],
    [2, 0, 4, 4],
    [4, 0, 0, 2]
]

# output(move_left(list02))
# output(move_top(list02))
output(move_end(list02))
